package com.lzy.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lzy.gmail.bean.UserAdress;
import com.lzy.gmail.service.OrderService;
import com.lzy.gmail.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

/*	Field userService in com.lzy.controller.OrderController required a bean of type
	'com.lzy.gmail.service.UserService' that could not be found.*/
//	@Autowired
//	@Reference(url = "127.0.0.0:20881",timeout = 3000, retries = 3, version = "1.0.0", stub = "com.lzy.service.impl.UserServiceImpl") // dubbo提供的，去远程发现从而自动注入
	@Reference(loadbalance = "random",timeout = 3000, retries = 3, version = "1.0.0", stub = "com.lzy.service.impl.UserServiceImpl") // dubbo提供的，去远程发现从而自动注入
	private UserService userService;

	@HystrixCommand(fallbackMethod = "hello") // 调用出现异常，调用hello方法
	@Override
	public List<UserAdress> initOrder(String userId) {
		// 1. 查询用户的地址
		System.out.println("用户id：" + userId);
		List<UserAdress> userAddressList = userService.getUserAddressList(userId);
//		for (UserAdress userAdress : userAddressList) {
//			System.out.println(userAdress.getUserAdress());
//		}
		return userAddressList;
	}

	public List<UserAdress> hello(String userId) {
		return Arrays.asList(new UserAdress(10, "测试地址", "10", "测试收货人", "测试电话", "测试默认"));
	}
}
